﻿' This software is licensed under Microsoft Public License (Ms-PL), which governs use of the accompanying software. 
' If you use the software, you accept this license. 

' The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, 
' guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot 
' change. To the extent permitted under your local laws, the contributors exclude the implied warranties of 
' merchantability, fitness for a particular purpose and non-infringement.

Imports System.ServiceModel.Web
Imports System.ServiceModel.Rest
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Dispatcher
Imports System.Runtime.Serialization

Namespace Dispatcher
    ''' <summary>
    ''' Control the fault message returned to the caller.
    ''' </summary>
    ''' <remarks></remarks>
    Friend NotInheritable Class RestErrorHandler
        Implements System.ServiceModel.Dispatcher.IErrorHandler

#Region "Constructor"
        ''' <summary>
        ''' Returns a new instance of the RestErrorHandler class.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()

        End Sub
#End Region

#Region "Events"
        ''' <summary>
        ''' Fired when error events are handled by the <see cref="RestErrorHandler">RestErrorHandler</see>.
        ''' </summary>
        ''' <param name="e">Exception to be handled.</param>
        ''' <remarks></remarks>
        Protected Friend Event OnHandleError(ByVal e As Exception)
#End Region

#Region "Public Methods"
        ''' <summary>
        ''' Returns a value that indicates whether the dispatcher aborts the session and the instance context in certain cases.
        ''' </summary>
        ''' <param name="error">Error to handle.</param>
        ''' <returns>True or false pending if the session should be keept alive.</returns>
        ''' <remarks></remarks>
        Public Function HandleError([error] As Exception) As Boolean Implements System.ServiceModel.Dispatcher.IErrorHandler.HandleError
            ' raise error event
            RaiseEvent OnHandleError([error])

            ' we keep the session alive.
            Return True
        End Function

        ''' <summary>
        ''' Enables the creation of a custom FaultException(Of TDetail) that is returned from an exception in the course of a service method.
        ''' </summary>
        ''' <param name="error">Error to inspect and provide.</param>
        ''' <param name="version">Version of the message.</param>
        ''' <param name="fault">Message of the fault.</param>
        ''' <remarks></remarks>
        Public Sub ProvideFault([error] As Exception, version As Channels.MessageVersion, ByRef fault As Channels.Message) Implements System.ServiceModel.Dispatcher.IErrorHandler.ProvideFault
           
        End Sub
#End Region
    End Class
End Namespace
